import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import cmpe273.classproject.server.dto.PersonResponse;
import cmpe273.classproject.server.model.Person;
import cmpe273.classproject.server.service.MovieServiceImplProxy;

public class CreateMemb extends javax.swing.JFrame {

    /** Creates new form CreateMemb */
    public CreateMemb() {
        initComponents();
    }
    
    public CreateMemb(AllSimple ui) {
        allSimple = ui;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        Fnm = new javax.swing.JTextField();
        FName = new javax.swing.JLabel();
        LName = new javax.swing.JLabel();
        Lnm = new javax.swing.JTextField();
        Address = new javax.swing.JLabel();
        Add1 = new javax.swing.JTextField();
        Add2 = new javax.swing.JTextField();
        city = new javax.swing.JLabel();
        cityNm = new javax.swing.JTextField();
        state = new javax.swing.JLabel();
        stateNm = new javax.swing.JComboBox();
        zip = new javax.swing.JLabel();
        zipCode = new javax.swing.JTextField();
        lblemail = new javax.swing.JLabel();
        emailaddress = new javax.swing.JTextField();
        membership = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        chkPremium = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("New Member Sign-Up");

        Fnm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FnmActionPerformed(evt);
            }
        });

        FName.setText("First Name");

        LName.setText("Last Name");

        Lnm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LnmActionPerformed(evt);
            }
        });

        Address.setText("Address ");

        city.setText("City");

        state.setText("State");

        zip.setText("Zip Code");
        
        lblemail.setText("Email Address");

        membership.setText("Premium?");
        
        String[] description = { "Alaska","Alabama","Arizona","Arkansas","California","Colorado", "Connecticut", "Delaware",
 				"Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana",
 				"Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana",
 				"Nebraska","Nevada"," New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota",
 				"Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota",
 				"Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming" }; 
        int count = 0;
        for (int i = 0; i < description.length; i++)
        {
        	stateNm.addItem(description[count++]);
        }

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(membership)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(LName)
                                .addComponent(FName)
                                .addComponent(Address)
                                .addComponent(city)
                                .addComponent(state)
                                .addComponent(zip)
                                .addComponent(lblemail))
                            .addGap(48, 48, 48)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            	.addComponent(emailaddress, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addComponent(zipCode, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addComponent(stateNm, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addComponent(cityNm, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addComponent(Add1, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addComponent(Lnm, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addComponent(Fnm, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addComponent(Add2, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(21, 21, 21)
                                    .addComponent(jButton1)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButton2))
                                .addComponent(chkPremium)))))
                .addGap(107, 107, 107))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(FName)
                    .addComponent(Fnm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lnm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LName))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Add1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Address))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Add2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cityNm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(city))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stateNm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(state))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zipCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(zip))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailaddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblemail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(membership)
                    .addComponent(chkPremium))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>

    private void FnmActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
    }                                   

    private void LnmActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
    }                                   

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
    	Pattern pEmailchecker = Pattern.compile("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$");
    	Matcher m5 = pEmailchecker.matcher(emailaddress.getText());
    	boolean emailchecker = m5.matches();
    	
    	Pattern pzipcodechecker = Pattern.compile("^[0-9]{5}$");
    	Matcher m6 = pzipcodechecker.matcher(zipCode.getText());
    	boolean zipcodechecker = m6.matches();
    	
    	
    	
    	if((ptextchecker(Fnm.getText())==false)||(Fnm.getText().length()<3)||( Fnm.getText().length()>15)||(Fnm.getText()=="")){ 
    		JOptionPane.showMessageDialog(null,"Invalid First Name");
    		}
    		else if((ptextchecker(Lnm.getText())==false)||(Lnm.getText().length()<3)||( Lnm.getText().length()>15)||(Lnm.getText()=="")){ 
    		JOptionPane.showMessageDialog(null,"Invalid Last Name");
    		}
    		else if((Add1.getText()=="")|| (Add1.getText().length()<6)|| (Add1.getText().length()>30)){ 
    		JOptionPane.showMessageDialog(null,"Invalid Address1");
    		}
    		
    		else if((cityNm.getText()=="")|| (cityNm.getText().length()<4)|| (cityNm.getText().length()>15)){
    		JOptionPane.showMessageDialog(null,"Invalid City");
    		}    		
    		else if((zipCode.getText()=="")|| (zipcodechecker==false)){
        	JOptionPane.showMessageDialog(null,"Invalid Zipcode");
        	}
        	else if((emailchecker==false)||(emailaddress.getText()=="")) {
        	JOptionPane.showMessageDialog(null,"Invalid Email Address");
        	} 
    		 	 
    		 else{
    			 createPerson();
    		    	
    			 allSimple.dispose();
    		    	allSimple = null;
    		    	
    		        if (allSimple == null)
    		            {
    		                allSimple = new AllSimple(this);
    		            }
    		        
    		        allSimple.setVisible(true);
    		        this.setVisible(false); 
    		        
    		 }
    		
	

    	
    	
    	
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        // Cancel
        
        if (allSimple == null)
            {
                allSimple = new AllSimple(this);
            }
        
        allSimple.setVisible(true);
        this.setVisible(false); 
        
    }

    public AllSimple allSimple = null; 
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CreateMemb().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField Add1;
    private javax.swing.JTextField Add2;
    private javax.swing.JLabel Address;
    private javax.swing.JLabel FName;
    private javax.swing.JTextField Fnm;
    private javax.swing.JLabel LName;
    private javax.swing.JTextField Lnm;
    private javax.swing.JCheckBox chkPremium;
    private javax.swing.JLabel city;
    private javax.swing.JTextField cityNm;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel membership;
    private javax.swing.JLabel state;
    private javax.swing.JComboBox stateNm;
    private javax.swing.JLabel zip;
    private javax.swing.JTextField zipCode;
    private javax.swing.JLabel lblemail;
    private javax.swing.JTextField emailaddress;
    // End of variables declaration
    
    private int createPerson()
    {
    	try
    	{
    		Person person = new Person();
    		//person.setMemberId(memberID);
    		person.setFirstName(Fnm.getText());
    		person.setLastName(Lnm.getText());
    		person.setAddress(Add1.getText());
    		person.setCity(cityNm.getText());
    		person.setState(stateNm.getSelectedItem().toString());
    		person.setZipCode(zipCode.getText());
    		person.setEmailId(emailaddress.getText());
    		
    		if(chkPremium.isSelected())
    		{
    			person.setType(2);
    			person.setBalance((float) 15.99);
    		}
    		else
    			person.setType(1);
    		
    		MovieServiceImplProxy proxy = new MovieServiceImplProxy();
    		int result = proxy.createPerson(person);    	
    		if (result == -1)
    			JOptionPane.showMessageDialog(this, "Member already exists!");
    		return result;
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    	
    	return -1;
    }
    
    public boolean ptextchecker(String str) {
       	for (int i = 0; i < str.length(); i++) {


                //If we find a non-digit character we return false.
                if (!Character.isLetter(str.charAt(i)))
                    return false;
            }
    	return true;
       	 
        }
}
